<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2017/7/12
 * Time: 15:19
 * Project name: PHPTEST
 * File name: 2.php
 * @copyright Copyright (c) 2016 ZRX Software LLC
 * @license https://github.com/zhangrxiang
 * @since 1.0.0
 */

const URL = "http://localhost/PHPTEST/2017/07/12/3.php";


//利用file_get_content来发送数据：
$data     = array(
	'name' => 'zhangrxiang',
	'age'  => 22
);
$postData = http_build_query( $data );
//echo $postData;
$opts    = array(
	'http' => array(
		'host'    => "localhost\r\n",
		'method'  => "POST",
		'header'  => "Content-type:application/x-www-form-urlencoded\r\n" . "Content-length:" . strlen( $postData ) . "\r\n",
		'content' => $postData
	)
);
$context = stream_context_create( $opts );
echo file_get_contents( URL, false, $context ).PHP_EOL;


// 1. 初始化curl会话
$ch = curl_init();
// 2. 设置
curl_setopt( $ch, CURLOPT_URL, URL );            //提交网址
curl_setopt( $ch, CURLOPT_POST, 1 );              //提交方式
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );    //提交数据
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );    //提交成功后返回数据字符串
// 3. 执行
$out_put = curl_exec( $ch );
// 4. 关闭会话
curl_close( $ch );
echo $out_put.PHP_EOL;


$data = array(
	'name'     => 'zhangrxiang',
	'age'     => 22
);
$postData = http_build_query($data);
$fp = fsockopen("localhost", 80, $errno, $errorStr, 5);
$request = "POST /PHPTEST/2017/07/12/3.php HTTP/1.1\r\n";
$request .= "Host:localhost\r\n";
$request .= "Content-type:application/x-www-form-urlencoded\r\n";
$request .= "Content-length:" . strlen($postData) . "\r\n";
$request .= "\r\n";
$request .= $postData;

fwrite($fp, $request);
while (!feof($fp)) {
	echo fgets($fp, 1024).PHP_EOL;
}
fclose($fp);